Home | History | Annotate | Download | only in importer
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Multiply-included message file, no traditonal include guard.
      6 #include <string>
      7 #include <vector>
      8 
      9 #include "base/basictypes.h"
     10 #include "base/string16.h"
     11 #include "base/values.h"
     12 #include "chrome/browser/history/history_types.h"
     13 #include "chrome/browser/importer/importer_data_types.h"
     14 #include "chrome/browser/importer/profile_writer.h"
     15 #include "chrome/browser/search_engines/template_url.h"
     16 #include "content/common/common_param_traits.h"
     17 #include "content/common/webkit_param_traits.h"
     18 #include "ipc/ipc_message_macros.h"
     19 #include "ipc/ipc_message_utils.h"
     20 #include "webkit/glue/password_form.h"
     21 
     22 #ifndef CHROME_BROWSER_IMPORTER_PROFILE_IMPORT_PROCESS_MESSAGES_H_
     23 #define CHROME_BROWSER_IMPORTER_PROFILE_IMPORT_PROCESS_MESSAGES_H_
     24 
     25 namespace IPC {
     26 
     27 // Traits for importer::SourceProfile struct to pack/unpack.
     28 template <>
     29 struct ParamTraits<importer::SourceProfile> {
     30   typedef importer::SourceProfile param_type;
     31   static void Write(Message* m, const param_type& p) {
     32     WriteParam(m, p.importer_name);
     33     WriteParam(m, static_cast<int>(p.importer_type));
     34     WriteParam(m, p.source_path);
     35     WriteParam(m, p.app_path);
     36     WriteParam(m, static_cast<int>(p.services_supported));
     37   }
     38   static bool Read(const Message* m, void** iter, param_type* p) {
     39     if (!ReadParam(m, iter, &p->importer_name))
     40       return false;
     41 
     42     int importer_type = 0;
     43     if (!ReadParam(m, iter, &importer_type))
     44       return false;
     45     p->importer_type = static_cast<importer::ImporterType>(importer_type);
     46 
     47     if (!ReadParam(m, iter, &p->source_path) ||
     48         !ReadParam(m, iter, &p->app_path))
     49         return false;
     50 
     51     int services_supported = 0;
     52     if (!ReadParam(m, iter, &services_supported))
     53       return false;
     54     p->services_supported = static_cast<uint16>(services_supported);
     55 
     56     return true;
     57   }
     58   static void Log(const param_type& p, std::string* l) {
     59     l->append("(");
     60     LogParam(p.importer_name, l);
     61     l->append(", ");
     62     LogParam(static_cast<int>(p.importer_type), l);
     63     l->append(", ");
     64     LogParam(p.source_path, l);
     65     l->append(", ");
     66     LogParam(p.app_path, l);
     67     l->append(", ");
     68     LogParam(static_cast<int>(p.services_supported), l);
     69     l->append(")");
     70   }
     71 };  // ParamTraits<importer::SourceProfile>
     72 
     73 // Traits for history::URLRow to pack/unpack.
     74 template <>
     75 struct ParamTraits<history::URLRow> {
     76   typedef history::URLRow param_type;
     77   static void Write(Message* m, const param_type& p) {
     78     WriteParam(m, p.id());
     79     WriteParam(m, p.url());
     80     WriteParam(m, p.title());
     81     WriteParam(m, p.visit_count());
     82     WriteParam(m, p.typed_count());
     83     WriteParam(m, p.last_visit());
     84     WriteParam(m, p.hidden());
     85   }
     86   static bool Read(const Message* m, void** iter, param_type* p) {
     87     history::URLID id;
     88     GURL url;
     89     string16 title;
     90     int visit_count, typed_count;
     91     base::Time last_visit;
     92     bool hidden;
     93     if (!ReadParam(m, iter, &id) ||
     94         !ReadParam(m, iter, &url) ||
     95         !ReadParam(m, iter, &title) ||
     96         !ReadParam(m, iter, &visit_count) ||
     97         !ReadParam(m, iter, &typed_count) ||
     98         !ReadParam(m, iter, &last_visit) ||
     99         !ReadParam(m, iter, &hidden))
    100       return false;
    101     *p = history::URLRow(url, id);
    102     p->set_title(title);
    103     p->set_visit_count(visit_count);
    104     p->set_typed_count(typed_count);
    105     p->set_last_visit(last_visit);
    106     p->set_hidden(hidden);
    107     return true;
    108   }
    109   static void Log(const param_type& p, std::string* l) {
    110     l->append("(");
    111     LogParam(p.id(), l);
    112     l->append(", ");
    113     LogParam(p.url(), l);
    114     l->append(", ");
    115     LogParam(p.title(), l);
    116     l->append(", ");
    117     LogParam(p.visit_count(), l);
    118     l->append(", ");
    119     LogParam(p.typed_count(), l);
    120     l->append(", ");
    121     LogParam(p.last_visit(), l);
    122     l->append(", ");
    123     LogParam(p.hidden(), l);
    124     l->append(")");
    125   }
    126 };  // ParamTraits<history::URLRow>
    127 
    128 // Traits for ProfileWriter::BookmarkEntry to pack/unpack.
    129 template <>
    130 struct ParamTraits<ProfileWriter::BookmarkEntry> {
    131   typedef ProfileWriter::BookmarkEntry param_type;
    132   static void Write(Message* m, const param_type& p) {
    133     WriteParam(m, p.in_toolbar);
    134     WriteParam(m, p.is_folder);
    135     WriteParam(m, p.url);
    136     WriteParam(m, p.path);
    137     WriteParam(m, p.title);
    138     WriteParam(m, p.creation_time);
    139   }
    140   static bool Read(const Message* m, void** iter, param_type* p) {
    141     return
    142         (ReadParam(m, iter, &p->in_toolbar)) &&
    143         (ReadParam(m, iter, &p->is_folder)) &&
    144         (ReadParam(m, iter, &p->url)) &&
    145         (ReadParam(m, iter, &p->path)) &&
    146         (ReadParam(m, iter, &p->title)) &&
    147         (ReadParam(m, iter, &p->creation_time));
    148   }
    149   static void Log(const param_type& p, std::string* l) {
    150     l->append("(");
    151     LogParam(p.in_toolbar, l);
    152     l->append(", ");
    153     LogParam(p.is_folder, l);
    154     l->append(", ");
    155     LogParam(p.url, l);
    156     l->append(", ");
    157     LogParam(p.path, l);
    158     l->append(", ");
    159     LogParam(p.title, l);
    160     l->append(", ");
    161     LogParam(p.creation_time, l);
    162     l->append(")");
    163   }
    164 };  // ParamTraits<ProfileWriter::BookmarkEntry>
    165 
    166 // Traits for history::ImportedFaviconUsage.
    167 template <>
    168 struct ParamTraits<history::ImportedFaviconUsage> {
    169   typedef history::ImportedFaviconUsage param_type;
    170   static void Write(Message* m, const param_type& p) {
    171     WriteParam(m, p.favicon_url);
    172     WriteParam(m, p.png_data);
    173     WriteParam(m, p.urls);
    174   }
    175   static bool Read(const Message* m, void** iter, param_type* p) {
    176     return
    177         ReadParam(m, iter, &p->favicon_url) &&
    178         ReadParam(m, iter, &p->png_data) &&
    179         ReadParam(m, iter, &p->urls);
    180   }
    181   static void Log(const param_type& p, std::string* l) {
    182     l->append("(");
    183     LogParam(p.favicon_url, l);
    184     l->append(", ");
    185     LogParam(p.png_data, l);
    186     l->append(", ");
    187     LogParam(p.urls, l);
    188     l->append(")");
    189   }
    190 };  // ParamTraits<history::ImportedFaviconUsage
    191 
    192 // Traits for TemplateURLRef
    193 template <>
    194 struct ParamTraits<TemplateURLRef> {
    195   typedef TemplateURLRef param_type;
    196   static void Write(Message* m, const param_type& p) {
    197     WriteParam(m, p.url());
    198     WriteParam(m, p.index_offset());
    199     WriteParam(m, p.page_offset());
    200   }
    201   static bool Read(const Message* m, void** iter, param_type* p) {
    202     std::string url;
    203     int index_offset;
    204     int page_offset;
    205     if (!ReadParam(m, iter, &url) ||
    206         !ReadParam(m, iter, &index_offset) ||
    207         !ReadParam(m, iter, &page_offset))
    208       return false;
    209     *p = TemplateURLRef(url, index_offset, page_offset);
    210     return true;
    211   }
    212   static void Log(const param_type& p, std::string* l) {
    213     l->append("<TemplateURLRef>");
    214   }
    215 };
    216 
    217 // Traits for TemplateURL::ImageRef
    218 template <>
    219 struct ParamTraits<TemplateURL::ImageRef> {
    220   typedef TemplateURL::ImageRef param_type;
    221   static void Write(Message* m, const param_type& p) {
    222     WriteParam(m, p.type);
    223     WriteParam(m, p.width);
    224     WriteParam(m, p.height);
    225     WriteParam(m, p.url);
    226   }
    227   static bool Read(const Message* m, void** iter, param_type* p) {
    228     std::string type;
    229     int width;
    230     int height;
    231     GURL url;
    232     if (!ReadParam(m, iter, &type) ||
    233         !ReadParam(m, iter, &width) ||
    234         !ReadParam(m, iter, &height) ||
    235         !ReadParam(m, iter, &url))
    236       return false;
    237     *p = TemplateURL::ImageRef(type, width, height, url);  // here in
    238     return true;
    239   }
    240   static void Log(const param_type& p, std::string* l) {
    241     l->append("<TemplateURL::ImageRef>");
    242   }
    243 };
    244 
    245 // Traits for TemplateURL
    246 template <>
    247 struct ParamTraits<TemplateURL> {
    248   typedef TemplateURL param_type;
    249   static void Write(Message* m, const param_type& p) {
    250     WriteParam(m, p.short_name());
    251     WriteParam(m, p.description());
    252     if (p.suggestions_url()) {
    253       WriteParam(m, true);
    254       WriteParam(m, *p.suggestions_url());
    255     } else {
    256       WriteParam(m, false);
    257     }
    258     WriteParam(m, *p.url());
    259     WriteParam(m, p.originating_url());
    260     WriteParam(m, p.keyword());
    261     WriteParam(m, p.autogenerate_keyword());
    262     WriteParam(m, p.show_in_default_list());
    263     WriteParam(m, p.safe_for_autoreplace());
    264     WriteParam(m, p.image_refs().size());
    265 
    266     std::vector<TemplateURL::ImageRef>::const_iterator iter;
    267     for (iter = p.image_refs().begin(); iter != p.image_refs().end(); ++iter) {
    268       WriteParam(m, iter->type);
    269       WriteParam(m, iter->width);
    270       WriteParam(m, iter->height);
    271       WriteParam(m, iter->url);
    272     }
    273 
    274     WriteParam(m, p.languages());
    275     WriteParam(m, p.input_encodings());
    276     WriteParam(m, p.date_created());
    277     WriteParam(m, p.usage_count());
    278     WriteParam(m, p.prepopulate_id());
    279   }
    280   static bool Read(const Message* m, void** iter, param_type* p) {
    281     string16 short_name;
    282     string16 description;
    283     bool includes_suggestions_url;
    284     TemplateURLRef suggestions_url;
    285     TemplateURLRef url;
    286     GURL originating_url;
    287     string16 keyword;
    288     bool autogenerate_keyword;
    289     bool show_in_default_list;
    290     bool safe_for_autoreplace;
    291     std::vector<string16> languages;
    292     std::vector<std::string> input_encodings;
    293     base::Time date_created;
    294     int usage_count;
    295     int prepopulate_id;
    296 
    297     if (!ReadParam(m, iter, &short_name) ||
    298         !ReadParam(m, iter, &description))
    299       return false;
    300 
    301     if (!ReadParam(m, iter, &includes_suggestions_url))
    302       return false;
    303     if (includes_suggestions_url) {
    304         if (!ReadParam(m, iter, &suggestions_url))
    305           return false;
    306     }
    307 
    308     size_t image_refs_size = 0;
    309     if (!ReadParam(m, iter, &url) ||
    310         !ReadParam(m, iter, &originating_url) ||
    311         !ReadParam(m, iter, &keyword) ||
    312         !ReadParam(m, iter, &autogenerate_keyword) ||
    313         !ReadParam(m, iter, &show_in_default_list) ||
    314         !ReadParam(m, iter, &safe_for_autoreplace) ||
    315         !ReadParam(m, iter, &image_refs_size))
    316       return false;
    317 
    318     *p = TemplateURL();
    319     for (size_t i = 0; i < image_refs_size; ++i) {
    320       std::string type;
    321       int width;
    322       int height;
    323       GURL url;
    324       if (!ReadParam(m, iter, &type) ||
    325           !ReadParam(m, iter, &width) ||
    326           !ReadParam(m, iter, &height) ||
    327           !ReadParam(m, iter, &url))
    328         return false;
    329       p->add_image_ref(TemplateURL::ImageRef(type, width, height, url));
    330     }
    331 
    332     if (!ReadParam(m, iter, &languages) ||
    333         !ReadParam(m, iter, &input_encodings) ||
    334         !ReadParam(m, iter, &date_created) ||
    335         !ReadParam(m, iter, &usage_count) ||
    336         !ReadParam(m, iter, &prepopulate_id))
    337       return false;
    338 
    339     p->set_short_name(short_name);
    340     p->set_description(description);
    341     p->SetSuggestionsURL(suggestions_url.url(), suggestions_url.index_offset(),
    342                          suggestions_url.page_offset());
    343     p->SetURL(url.url(), url.index_offset(), url.page_offset());
    344     p->set_originating_url(originating_url);
    345     p->set_keyword(keyword);
    346     p->set_autogenerate_keyword(autogenerate_keyword);
    347     p->set_show_in_default_list(show_in_default_list);
    348     p->set_safe_for_autoreplace(safe_for_autoreplace);
    349 
    350     std::vector<string16>::const_iterator lang_iter;
    351     for (lang_iter = languages.begin();
    352          lang_iter != languages.end();
    353          ++lang_iter) {
    354       p->add_language(*lang_iter);
    355     }
    356     p->set_input_encodings(input_encodings);
    357     p->set_date_created(date_created);
    358     p->set_usage_count(usage_count);
    359     p->set_prepopulate_id(prepopulate_id);
    360     return true;
    361   }
    362   static void Log(const param_type& p, std::string* l) {
    363     l->append("<TemplateURL>");
    364   }
    365 };
    366 
    367 }  // namespace IPC
    368 
    369 #endif  // CHROME_BROWSER_IMPORTER_PROFILE_IMPORT_PROCESS_MESSAGES_H_
    370 
    371 #define IPC_MESSAGE_START ProfileImportMsgStart
    372 
    373 //-----------------------------------------------------------------------------
    374 // ProfileImportProcess messages
    375 // These are messages sent from the browser to the profile import process.
    376 IPC_MESSAGE_CONTROL4(ProfileImportProcessMsg_StartImport,
    377                      importer::SourceProfile,
    378                      int                     /* Bitmask of items to import. */,
    379                      DictionaryValue         /* Localized strings. */,
    380                      bool                    /* Import to bookmark bar. */)
    381 
    382 IPC_MESSAGE_CONTROL0(ProfileImportProcessMsg_CancelImport)
    383 
    384 IPC_MESSAGE_CONTROL1(ProfileImportProcessMsg_ReportImportItemFinished,
    385                      int  /* ImportItem */)
    386 
    387 //---------------------------------------------------------------------------
    388 // ProfileImportProcessHost messages
    389 // These are messages sent from the profile import process to the browser.
    390 // These messages send information about the status of the import and
    391 // individual import tasks.
    392 IPC_MESSAGE_CONTROL0(ProfileImportProcessHostMsg_Import_Started)
    393 
    394 IPC_MESSAGE_CONTROL2(ProfileImportProcessHostMsg_Import_Finished,
    395                      bool         /* was import successful? */,
    396                      std::string  /* error message, if any */)
    397 
    398 IPC_MESSAGE_CONTROL1(ProfileImportProcessHostMsg_ImportItem_Started,
    399                      int  /* ImportItem */)
    400 
    401 IPC_MESSAGE_CONTROL1(ProfileImportProcessHostMsg_ImportItem_Finished,
    402                      int  /* ImportItem */)
    403 
    404 // These messages send data from the external importer process back to
    405 // the process host so it can be written to the profile.
    406 IPC_MESSAGE_CONTROL1(ProfileImportProcessHostMsg_NotifyHistoryImportStart,
    407                      int  /* total number of history::URLRow items */)
    408 
    409 IPC_MESSAGE_CONTROL2(ProfileImportProcessHostMsg_NotifyHistoryImportGroup,
    410                      std::vector<history::URLRow>,
    411                      int  /* the source of URLs as in history::VisitSource.*/
    412                           /* To simplify IPC call, pass as an integer */)
    413 
    414 IPC_MESSAGE_CONTROL1(ProfileImportProcessHostMsg_NotifyHomePageImportReady,
    415                      GURL  /* GURL of home page */)
    416 
    417 IPC_MESSAGE_CONTROL3(ProfileImportProcessHostMsg_NotifyBookmarksImportStart,
    418                      string16  /* first folder name */,
    419                      int       /* options */,
    420                      int       /* total number of bookmarks */)
    421 
    422 IPC_MESSAGE_CONTROL1(ProfileImportProcessHostMsg_NotifyBookmarksImportGroup,
    423                      std::vector<ProfileWriter::BookmarkEntry>)
    424 
    425 IPC_MESSAGE_CONTROL1(ProfileImportProcessHostMsg_NotifyFaviconsImportStart,
    426                      int  /* total number of favicons */)
    427 
    428 IPC_MESSAGE_CONTROL1(ProfileImportProcessHostMsg_NotifyFaviconsImportGroup,
    429                      std::vector<history::ImportedFaviconUsage>)
    430 
    431 IPC_MESSAGE_CONTROL1(ProfileImportProcessHostMsg_NotifyPasswordFormReady,
    432                      webkit_glue::PasswordForm)
    433 
    434 IPC_MESSAGE_CONTROL3(ProfileImportProcessHostMsg_NotifyKeywordsReady,
    435                      std::vector<TemplateURL>,
    436                      int,  /* default keyword index */
    437                      bool  /* unique on host and path */)
    438