summaryrefslogtreecommitdiff
path: root/BCT/BytecodeTranslator/Phone/PhoneNavigationTraverser.cs
blob: b702e78bffa7b4966302ca083cfefafeead1ebda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Cci;
using Microsoft.Cci.MutableCodeModel;
using TranslationPlugins;

namespace BytecodeTranslator.Phone {
  public class PhoneNavigationCodeTraverser : BaseCodeTraverser {
    private MetadataReaderHost host;
    private ITypeReference navigationSvcType;
    private ITypeReference typeTraversed;
    private PhoneControlsPlugin phonePlugin;

    private static readonly string[] NAV_CALLS = { "GoBack", "GoForward", "Navigate", "StopLoading" };

    public PhoneNavigationCodeTraverser(PhoneControlsPlugin plugin, MetadataReaderHost host, ITypeReference typeTraversed) : base() {
      this.host = host;
      this.typeTraversed = typeTraversed;
      this.phonePlugin = plugin;
      Microsoft.Cci.Immutable.PlatformType platform = host.PlatformType as Microsoft.Cci.Immutable.PlatformType;

      // TODO obtain version, culture and signature data dynamically
      AssemblyIdentity MSPhoneAssemblyId =
          new AssemblyIdentity(host.NameTable.GetNameFor("Microsoft.Phone"), "", new Version("7.0.0.0"),
                               new byte[] { 0x24, 0xEE, 0xC0, 0xD8, 0xC8, 0x6C, 0xDA, 0x1E }, "");

      IAssembly phoneAssembly = host.FindAssembly(MSPhoneAssemblyId);

      // TODO determine the needed types dynamically
      navigationSvcType = platform.CreateReference(phoneAssembly, "System", "Windows", "Navigation", "NavigationService");
    }

    public override void Visit(IMethodDefinition method) {
      if (method.IsConstructor && PhoneCodeHelper.isPhoneApplicationClass(typeTraversed, host)) {
        // TODO initialize current navigation URI to mainpage, using a placeholder for now.
        // TODO BUG doing this is generating a fresh variable definition somewhere that the BCT then translates into two different (identical) declarations
        string mainPageUri = phonePlugin.getMainPageXAML();
        SourceMethodBody sourceBody = method.Body as SourceMethodBody;
        if (sourceBody != null) {
          BlockStatement bodyBlock = sourceBody.Block as BlockStatement;
          if (bodyBlock != null) {
            Assignment uriInitAssign = new Assignment() {
              Source = new CompileTimeConstant() {
                Type = host.PlatformType.SystemString,
                Value = mainPageUri,
              },
              Type = host.PlatformType.SystemString,
              Target = new TargetExpression() {
                Type = host.PlatformType.SystemString,
                Definition = new FieldReference() {
                  ContainingType= typeTraversed,
                  IsStatic=true,
                  Type=host.PlatformType.SystemString,
                  Name=host.NameTable.GetNameFor(PhoneCodeHelper.IL_CURRENT_NAVIGATION_URI_VARIABLE),
                },
              },
            };
            Statement uriInitStmt= new ExpressionStatement() {
              Expression= uriInitAssign,
            };
            bodyBlock.Statements.Insert(0, uriInitStmt);
          }
        }
      }
      base.Visit(method);
    }


    private bool navCallFound=false;
    private StaticURIMode currentStaticMode= StaticURIMode.NOT_STATIC;
    private string unpurifiedFoundURI="";

    public override void Visit(IBlockStatement block) {
      IList<Tuple<IStatement,StaticURIMode,string>> navStmts = new List<Tuple<IStatement,StaticURIMode,string>>();
      foreach (IStatement statement in block.Statements) {
        navCallFound = false;
        this.Visit(statement);
        if (navCallFound) {
          navStmts.Add(new Tuple<IStatement, StaticURIMode, string>(statement, currentStaticMode, unpurifiedFoundURI));
        }
      }

      injectNavigationUpdateCode(block, navStmts);
    }

    public override void Visit(IMethodCall methodCall) {
      // check whether it is a NavigationService call
      IMethodReference methodToCall= methodCall.MethodToCall;
      ITypeReference callType= methodToCall.ContainingType;
      if (!callType.ResolvedType.Equals(navigationSvcType.ResolvedType))
        return;

      string methodToCallName= methodToCall.Name.Value;
      if (!NAV_CALLS.Contains(methodToCallName))
        return;

      // TODO check what to do with these
      if (methodToCallName == "GoForward" || methodToCallName == "StopLoading") {
        // TODO forward navigation is not supported by the phone
        // TODO StopLoading is very async, I don't think we may verify this behaviour
        // TODO possibly log
        return;
      } else {
        bool isStatic=false;
        currentStaticMode = StaticURIMode.NOT_STATIC;

        if (methodToCallName == "GoBack")
          isStatic= true;
        else { // Navigate()
          // check for different static patterns that we may be able to verify
          IExpression uriArg= methodCall.Arguments.First();
          if (isArgumentURILocallyCreatedStatic(uriArg, out unpurifiedFoundURI)) {
            isStatic = true;
            currentStaticMode = StaticURIMode.STATIC_URI_CREATION_ONSITE;
          } else if (isArgumentURILocallyCreatedStaticRoot(uriArg, out unpurifiedFoundURI)) {
            isStatic = true;
            currentStaticMode = StaticURIMode.STATIC_URI_ROOT_CREATION_ONSITE;
          } else {
            // get reason
            //ICreateObjectInstance creationSite = methodCall.Arguments.First() as ICreateObjectInstance;
            //if (creationSite == null)
            //  notStaticReason = "URI not created at call site";
            //else
            //  notStaticReason = "URI not initialized as a static string";
          }
        }

        if (isStatic)
          navCallFound = true;

        //Console.Write("Page navigation event found. Target is static? " + (isStatic ? "YES" : "NO"));
        //if (!isStatic) {
        //  Console.WriteLine(" -- Reason: " + notStaticReason);
        //} else {
        //  Console.WriteLine("");
        //}
      }
    }
    
    /// <summary>
    /// checks if argument is locally created URI with static URI target
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    private bool isArgumentURILocallyCreatedStatic(IExpression arg, out string uri) {
      uri = null;
      if (!arg.isCreateObjectInstance())
        return false;

      if (!arg.Type.isURIClass(host))
        return false;

      ICreateObjectInstance creationSite = arg as ICreateObjectInstance;
      IExpression uriTargetArg= creationSite.Arguments.First();

      if (!uriTargetArg.Type.isStringClass(host))
        return false;

      ICompileTimeConstant staticURITarget = uriTargetArg as ICompileTimeConstant;
      if (staticURITarget == null)
        return false;

      uri= staticURITarget.Value as string;
      return true;
    }

    /// <summary>
    /// checks if argument is locally created URI where target has statically created URI root
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    private bool isArgumentURILocallyCreatedStaticRoot(IExpression arg, out string uri) {
      // Pre: !isArgumentURILocallyCreatedStatic
      uri = null;
      if (!arg.isCreateObjectInstance())
        return false;

      if (!arg.Type.isURIClass(host))
        return false;

      ICreateObjectInstance creationSite = arg as ICreateObjectInstance;
      IExpression uriTargetArg = creationSite.Arguments.First();

      if (!uriTargetArg.Type.isStringClass(host))
        return false;

      if (!uriTargetArg.IsStaticURIRootExtractable(out uri))
        return false;

      return true;
    }

    private void injectNavigationUpdateCode(IBlockStatement block, IEnumerable<Tuple<IStatement,StaticURIMode, string>> stmts) {
      // TODO Here there is the STRONG assumption that a given method will only navigate at most once per method call
      // TODO (or at most will re-navigate to the same page). Quick "page flipping" on the same method
      // TODO would not be captured correctly
      foreach (Tuple<IStatement, StaticURIMode, string> entry in stmts) {
        int ndx= block.Statements.ToList().IndexOf(entry.Item1, 0);
        if (ndx == -1) {
          // can't be
          throw new IndexOutOfRangeException("Statement must exist in original block");
        }

        Assignment currentURIAssign = new Assignment() {
          Source = new CompileTimeConstant() {
            Type = host.PlatformType.SystemString,
            Value = PhoneCodeHelper.getURIBase(entry.Item3),
          },
          Type = host.PlatformType.SystemString,
          Target = new TargetExpression() {
            Type = host.PlatformType.SystemString,
            Definition = new FieldReference() {
              ContainingType = typeTraversed,
              IsStatic = true,
              Type = host.PlatformType.SystemString,
              Name = host.NameTable.GetNameFor(PhoneCodeHelper.IL_CURRENT_NAVIGATION_URI_VARIABLE),
            },
          },
        };
        Statement uriInitStmt = new ExpressionStatement() {
          Expression = currentURIAssign,
        };
        block.Statements.ToList().Insert(ndx+1, uriInitStmt);
      }
    }
  }

  /// <summary>
  /// Traverse metadata looking only for PhoneApplicationPage's constructors
  /// </summary>
  public class PhoneNavigationMetadataTraverser : BaseMetadataTraverser {
    private MetadataReaderHost host;
    private ITypeDefinition typeBeingTraversed;
    private PhoneControlsPlugin phonePlugin;

    public PhoneNavigationMetadataTraverser(PhoneControlsPlugin plugin, MetadataReaderHost host)
      : base() {
      this.host = host;
      this.phonePlugin = plugin;
    }

    public override void Visit(IModule module) {
      base.Visit(module);
    }

    public override void Visit(IAssembly assembly) {
      base.Visit(assembly);
    }

    // TODO can we avoid visiting every type? Are there only a few, identifiable, types that may perform navigation?
    public override void Visit(ITypeDefinition typeDefinition) {
      typeBeingTraversed = typeDefinition;
      if (typeDefinition.isPhoneApplicationClass(host)) {
        NamespaceTypeDefinition mutableTypeDef = typeDefinition as NamespaceTypeDefinition;
        if (mutableTypeDef != null) {
          FieldDefinition fieldDef = new FieldDefinition() {
            ContainingTypeDefinition= mutableTypeDef,
            InternFactory= host.InternFactory,
            IsStatic= true,
            Name= host.NameTable.GetNameFor(PhoneCodeHelper.IL_CURRENT_NAVIGATION_URI_VARIABLE),
            Type= host.PlatformType.SystemString,
            Visibility= TypeMemberVisibility.Public,
          };
          mutableTypeDef.Fields.Add(fieldDef);
        }
      }
      base.Visit(typeDefinition);
    }

    // TODO same here. Are there specific methods (and ways to identfy those) that can perform navigation?
    public override void Visit(IMethodDefinition method) {

      PhoneNavigationCodeTraverser codeTraverser = new PhoneNavigationCodeTraverser(phonePlugin, host, typeBeingTraversed);
      codeTraverser.Visit(method);
    }

    public void InjectPhoneCodeAssemblies(IEnumerable<IUnit> assemblies) {
      foreach (var a in assemblies) {
        a.Dispatch(this);
      }
    }
  }
}